var aFrame = [];
var frameAction = null
function showFrame(action){
    frameAction = action;
    if (!$('#_body')[0]) {
        $('body').append('<div id="_body"></div>');
        $('#_body').css({
            'z-index': 9999,
            position: 'absolute',
            background: '#cccccc',
            filter: 'alpha(opacity=20)',
            '-moz-opacity': 0.2,
            width: document.body.scrollWidth,
            height: document.body.scrollHeight < document.body.clientHeight ? document.body.clientHeight : document.body.scrollHeight,
            top: 0,
            left: 0
        });
    }
    if (!$('#_frame')[0]) {
        $('body').append('<div id="_frame" class="_frame"><div id="_frame_tools"><a onclick="closeFrame()">关闭</a></div><div id="_frame_body"></div></div>');
    }
    if (typeof aFrame[name] == 'undefined') {
        $.get('?a=frame&m=add', writeFrame);
    }
}

function writeFrame(data){
    $('#_frame_body').html(data);
    $('#_frame_body div:first-child').css({
        'overflow': 'auto'
    });
    var frameWidth = $('#_frame_body div').width() + 8;
    var frameHeight = $('#_frame_body div').height() + 30;
    var frameLeft = (document.body.clientWidth - frameWidth) / 2;
    var frameTop = (document.body.clientHeight - frameHeight) / 2;
    $('#_frame').css({
        'z-index': 10000,
        position: 'absolute',
        background: '#fff',
        border: '4px solid #6694E3',
        width: frameWidth,
        height: frameHeight,
        left: frameLeft,
        top: frameTop < 0 ? 0 : frameTop
    });
    $('#_frame_tools').css({
        'line-height': '20px',
        'text-align': 'right'
    });
}

function closeFrame(){
    $('#_frame').remove();
    $('#_body').remove();
    frameName = null;
}

function submitFrame(){
    var posts = {};
    $('input', $('#_frame_body')).each(function(i){
        var name = $(this).attr('name');
        if (name != '_frameAction') {
            var type = $(this).attr('type');
            switch (type) {
                case 'text':
                case 'password':
                case 'hidden':
                    if (typeof posts[name] == 'undefined') {
                        posts[name] = $(this).val();
                    }
                    else {
                        posts[name] += ';' + $(this).val();
                    }
                    break;
                case 'radio':
                case 'checkbox':
                    if ($(this).attr('checked')) {
                        if (typeof posts[name] == 'undefined') {
                            posts[name] = $(this).val();
                        }
                        else {
                            posts[name] += ';' + $(this).val();
                        }
                    }
                    break;
            }
        }
        else {
            url = $(this).val();
        }
    })
    $('textarea', $('#_frame_body')).each(function(i){
        var name = $(this).attr('name');
        var type = $(this).attr('type');
        posts[name] = $(this).val();
    })
    if (url == '') {
        url = frameAction;
    }
    $.post(url, posts, writeFrame);
}

function uploadFram(obj){
    var uploadId = '_upload_' + (new Date).getTime();
    $(obj.form).attr({
        target: uploadId,
        id: '_form_' + uploadId
    }).append('<input type="hidden" name="uploadId" value="' + uploadId + '"><iframe name="' + uploadId + '" frameborder="0" height="0"></ifrmae>');
    obj.form.submit();
    $(obj).before('<span id="_message_' + uploadId + '">正在上传</span><a href="#" onclick="cancelUpload(\'' + uploadId + '\')">取消</a>');
    $(obj.form).after('<form action="?a=item&m=upload&type=image" method="post" enctype="multipart/form-data"><input type="file" name="file" onchange="uploadFram(this)"></form>');
    $(obj).remove();
}

function cancelUpload(id){
    $('#_form_' + id).remove();
}

var dropdrawId = null;
function dropdraw(obj){
    $('ul', $(obj)).toggle();
    if ($('ul', $(obj)).css('display') == 'block') {
        dropdrawId = getAttr(obj, 'id', 0);
        var off = $(obj).offset();
        $('ul', $(obj)).css({
            'left': off.left,
            'top': $(obj).height() + off.top + 3
        })
    }
    else {
        dropdrawId = null;
    }
}

$(document).ready(function(){
    $('body').click(function(e){
        if (dropdrawId != null) {
            if ($.browser.msie) {
                var id = getAttr(e.srcElement, 'id', 0);
            }
            else {
                var id = getAttr(e.target, 'id', 0);
            }
            if (id != dropdrawId) {
                $('ul.dropdraw').css('display', 'none');
            }
        }
    })
    $('ul.dropdraw').parent().css({
        cursor: 'pointer'
    }).click(function(){
        eval('dropdraw(this)');
    });
})

function getAttr(obj, attr, level){
    var val = $(obj).attr(attr);
    if (val) {
        return val;
    }
    else 
        if (level == 3) {
            return false;
        }
        else {
            val = getAttr($(obj).parent(), attr, level + 1);
            return val;
        }
}

var bScrollMoveTime = 0;
function scrollMove(){
    var iArgNum = arguments.length;
    if (iArgNum == 0) {
        speed = 10;
        bScrollMoveTime = (new Date()).getTime();
    }
    
    if (bScrollMoveTime + 100 > (new Date()).getTime()) {
        return;
    }
    var arg = ['id', 'type', 'speed'];
    for (var i = 0; i < iArgNum; i++) {
        eval(arg[i] + '="' + arguments[i] + '";');
    }
    if (typeof speed == 'undefined') {
        speed = 10;
    }
    speed *= 1;
    obj = $('#' + id);
    switch (type) {
        case 'left':
            obj.scrollLeft(obj.scrollLeft() - speed);
            break;
        case 'right':
            obj.scrollLeft(obj.scrollLeft() + speed);
            break;
    }
    
    speed += 10;
    setTimeout('scrollMove("' + id + '", "' + type + '", ' + speed + ')', 100);
}

var s = "";
s += "\r\n网页可见区域宽：" + document.body.clientWidth;
s += "\r\n网页可见区域高：" + document.body.clientHeight;
s += "\r\n网页可见区域宽：" + document.body.offsetWidth + " (包括边线的宽)";
s += "\r\n网页可见区域高：" + document.body.offsetHeight + " (包括边线的宽)";
s += "\r\n网页正文全文宽：" + document.body.scrollWidth;
s += "\r\n网页正文全文高：" + document.body.scrollHeight;
s += "\r\n网页被卷去的高：" + document.body.scrollTop;
s += "\r\n网页被卷去的左：" + document.body.scrollLeft;
s += "\r\n网页正文部分上：" + window.screenTop;
s += "\r\n网页正文部分左：" + window.screenLeft;
s += "\r\n屏幕分辨率的高：" + window.screen.height;
s += "\r\n屏幕分辨率的宽：" + window.screen.width;
s += "\r\n屏幕可用工作区高度：" + window.screen.availHeight;
s += "\r\n屏幕可用工作区宽度：" + window.screen.availWidth;
//alert(s);
